﻿window.onload = function () {
    for (var i = 0; i < 7; i++)
    {
        new JsDatePick({
            useMode: 2,
            target: "DateField" + i,
            cellColorScheme: "pink",
            //dateFormat:"%m-%d-%Y",
            imgPath: "img/"
        });
    }
};

summary =
{
    AddDate: function()
    {
        alert('test');
        var e = document.getElementById("MondayStart");
        alert(e);
        var val = e.options[e.selectedIndex].text;
        alert(val);


        var url = '@Url.Action("Foo", "Hours")';
        var data = { value: val};
        $.post(url, data, function () {
            alert('woot');
        });


        alert('done');
    },

    AutoFillDate: function(id)
    {
        var date = document.getElementById('DateField' + id);
        if (date.value == "") date.value = summary.GetDate();
    },

    GetDate: function()
    {
        var today = new Date();
        today.setDate(today.getDate() + 1);
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!

        var yyyy = today.getFullYear();
        if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = mm+'-'+dd+'-'+yyyy;
        return today;
    },

    ToggleDate: function(e)
    {
        alert(e);
    },

    ToggleNewDate: function() 
    {
        var ele = document.getElementById('ShowNewDate');
        var instructions = document.getElementById('ShowNewDateInsturctions');
        var newDateButton = document.getElementById('addNewDateButton');
        if (ele.style.display == 'block')
        {
            ele.style.display = 'none';
            newDateButton.style.display = 'block';
            newDateButton.value = 'Add effective date';
            instructions.style.display = 'none';
        }
        else
        {
            ele.style.display = 'block';
            instructions.style.display = 'block';
            newDateButton.style.display = 'none';
            var row = document.getElementById('GridView_DXDataRow0');
            var array = row.innerHTML.split("<td class=\"dxgv\">");
            ///alert(array[14]);
            for (var i = 2; i < array.length; i+=2)
            {
                array[i] = array[i].replace("<br>", '-');
                var time = array[i].substring(0, array[i].indexOf('<'));
                summary.SetTime(i,time);
            }
        }
    },

    SetTime: function(i,timeString)
    {
        var times = -1;
        var start = -1;
        var startminMir = -1;
        var end = -1;
        var endminMir = -1;

        if (timeString !== 'Closed')
        {
            times = timeString.split('-');
            start = times[0].split(':');
            startminMir = start[1].split(' ');
            end = times[1].split(':');
            endminMir = end[1].split(' ');
        }
        var day;
        if (i == 2) day = 'Monday';
        else if (i == 3) day = 'Tuesday';
        else if (i == 4) day = 'Wednesday'
        else if (i == 5) day = 'Thursday';
        else if (i == 6) day = 'Friday';
        else if (i == 7) day = 'Saturday';
        else if (i == 8) day = 'Sunday';
        document.getElementById('start' + day + 'Hour').value = parseInt(start[0]);
        document.getElementById('start' + day + 'Minute').value = parseInt(startminMir[0]);
        document.getElementById('start' + day + 'Meridian').value = startminMir[1];
        document.getElementById('end' + day + 'Hour').value = parseInt(end[0]);
        document.getElementById('end' + day + 'Minute').value = parseInt(endminMir[0]);
        document.getElementById('end' + day + 'Meridian').value = endminMir[1];

    },

    Submit: function ()
    {
        return summary.DatesHaveData();
    },

    validate: function()
    {
        var check = false;
        for(var i = 0; i < 7 ; i++)
        {
            var day;
            if (i == 0) day = 'Monday';
            else if (i == 1) day = 'Tuesday';
            else if (i == 2) day = 'Wednesday'
            else if (i == 3) day = 'Thursday';
            else if (i == 4) day = 'Friday';
            else if (i == 5) day = 'Saturday';
            else if (i == 6) day = 'Sunday';
            if (document.getElementById('start' + day + 'Meridian').value == "PM" &&
            document.getElementById('end' + day + 'Meridian').value == "AM") check = true;
        }
        if (check)
        {
            return confirm('Are these dates correct?');
        }
        return true;
    },

    DatesHaveData: function()
    {
        if(!summary.validate())return false;
        for (var i = 0; i < 7; i++) 
        {
            if (new Date(document.getElementById("DateField" + i).value).getTime() < new Date().getTime())
            {
                alert('Please enter a date after today');
                return false;
            }
            if (document.getElementById("DateField" + i).value != "") return true;
        }
        alert('Please enter an effective date');
        return false;
    }
}
